package com.gong.gongcloudpicture1.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gong.gongcloudpicture1.annotation.AuthCheck;
import com.gong.gongcloudpicture1.common.BaseResponse;
import com.gong.gongcloudpicture1.common.DeleteRequest;
import com.gong.gongcloudpicture1.constant.UserConstant;
import com.gong.gongcloudpicture1.exception.BusinessException;
import com.gong.gongcloudpicture1.exception.ErrorCode;
import com.gong.gongcloudpicture1.model.dto.user.*;
import com.gong.gongcloudpicture1.model.entity.user.User;
import com.gong.gongcloudpicture1.model.vo.user.UserLoginVo;
import com.gong.gongcloudpicture1.model.vo.user.UserVO;
import com.gong.gongcloudpicture1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户 接口层
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/userRegister")
    @AuthCheck(mastRole = UserConstant.ADMIN)
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return userService.userRegister(userRegisterRequest);
    }


    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request          该参数需要给用塞一个sessionId
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public BaseResponse<UserLoginVo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return userService.login(userLoginRequest, request);
    }


    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    @ResponseBody
    public BaseResponse<UserLoginVo> getLogInUser(HttpServletRequest request) {
        return userService.getLogInUser(request);
    }


    /**
     * 注销用户
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> logout(HttpServletRequest request) {
        return userService.logout(request);
    }


    /**
     * 创建用户
     *
     * @param userAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mastRole = UserConstant.ADMIN)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
        if (userAddRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return userService.addUser(userAddRequest);
    }


    /**
     * 根据id获取用户 (管理员操作)
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mastRole = UserConstant.ADMIN)
    public BaseResponse<User> getUserById(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return userService.getUserById(id);
    }


    /**
     * 通过id获取用户脱敏信息
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVoById(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        User user = getUserById(id).getData();
        return BaseResponse.success(userService.getUserVo(user));
    }


    /**
     * 删除用户
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mastRole = UserConstant.ADMIN)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return BaseResponse.success(userService.removeById(deleteRequest.getId()));
    }


    /**
     * 更新用户
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
        if (userUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userService.updateUser(userUpdateRequest);
    }



    /**
     * 用户列表分页
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mastRole = UserConstant.ADMIN)
    @ResponseBody
    public BaseResponse<Page<UserVO>> listUserVoByPage(@RequestBody UserQueryRequest userQueryRequest) {
        if (userQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return userService.listUserVoByPage(userQueryRequest);
    }



}
